#include<vector>
#include<unistd.h>
#include<string>
#include<pthread.h>
#include<time.h>
#include<iostream>
#include"blockQueue.hpp"
using namespace std;
void* Consumer(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*) arg;
    while(true)
    {
        Task t;
        bq->pop(t);
        t();
        cout<<"Consumer:"<<t.getRformat()<<endl;
        sleep(1);
    }
}
void* Productor(void* arg)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*) arg;
    string format = "+-*/%";
    while(true)
    {
        int x = rand() % 10;
        int y = rand() % 10;
        char op = format[rand()%format.size()];
        Task t(x,y,op);
        cout<<"Productor: "<<t.getformat()<<endl;;
        bq->push(t);
        sleep(1);
    }
    return nullptr;
}
int main()
{
    BlockQueue<Task> bq;
    srand(time(nullptr));
    pthread_t c[3],p[3];
    pthread_create(&c[0],nullptr,Consumer,&bq);
    pthread_create(&c[1],nullptr,Consumer,&bq);
    pthread_create(&c[2],nullptr,Productor,&bq);
    pthread_create(&p[0],nullptr,Productor,&bq);
    pthread_create(&p[1],nullptr,Productor,&bq);
    pthread_create(&p[2],nullptr,Productor,&bq);

    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);

    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);
    pthread_join(p[2],nullptr);

    return 0;
}